package com.blue.firecat.utils.money;

import java.math.BigDecimal;

/**
 * BigDecimal 工具类
 *
 * @author chenjiusheng
 * @since 2017年1月10日 下午10:04:49
 */
public class BigDecimalUtil {

    private static final int DEFAULT_SCALE = 2;
    private static final int DEFAULT_ROUND_MODE = BigDecimal.ROUND_HALF_UP;

    // 这个类不能实例化
    private BigDecimalUtil() {
    }

    /**
     * BigDecimal 加法
     *
     * @param params
     * @return
     */
    public static BigDecimal add(BigDecimal... params) {
        if (params == null || params.length == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal total = BigDecimal.ZERO;
        for (BigDecimal param : params) {
            if (param != null) {
                total = total.add(param);
            }
        }
        return total;
    }

    /**
     * double 加法
     *
     * @param params
     * @return
     */
    public static BigDecimal add(double... params) {
        if (params == null || params.length == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal total = BigDecimal.ZERO;
        for (double param : params) {
            total = total.add(new BigDecimal(Double.toString(param)));
        }
        return total;
    }

    /**
     * BigDecimal 减法
     *
     * @param params
     * @return
     */
    public static BigDecimal subtract(BigDecimal... params) {
        if (params == null || params.length == 0 || params[0] == null) {
            return BigDecimal.ZERO;
        }
        BigDecimal total = params[0];
        for (int i = 1, len = params.length; i < len; i++) {
            if (params[i] != null) {
                total = total.subtract(params[i]);
            }
        }
        return total;
    }

    /**
     * double 减法
     *
     * @param params
     * @return
     */
    public static BigDecimal subtract(double... params) {
        if (params == null || params.length == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal total = new BigDecimal(Double.toString(params[0]));
        for (int i = 1, len = params.length; i < len; i++) {
            total = total.subtract(new BigDecimal(Double.toString(params[i])));
        }
        return total;
    }


    /**
     * BigDecimal 乘法
     *
     * @param params
     * @return
     */
    public static BigDecimal multiply(BigDecimal... params) {
        if (params == null || params.length == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal total = BigDecimal.ONE;
        for (BigDecimal param : params) {
            if (param != null) {
                total = total.multiply(param);
            }
        }
        return total.setScale(DEFAULT_SCALE, DEFAULT_ROUND_MODE);
    }

    /**
     * double 乘法
     *
     * @param params
     * @return
     */
    public static BigDecimal multiply(double... params) {
        if (params == null || params.length == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal total = BigDecimal.ONE;
        for (double param : params) {
            total = total.multiply(new BigDecimal(Double.toString(param)));
        }
        return total.setScale(DEFAULT_SCALE, DEFAULT_ROUND_MODE);
    }


    /**
     * BigDecimal 减法
     *
     * @param params
     * @return
     */
    public static BigDecimal divide(BigDecimal... params) {
        if (params == null || params.length == 0 || params[0] == null) {
            return BigDecimal.ZERO;
        }
        BigDecimal total = params[0];
        for (int i = 1, len = params.length; i < len; i++) {
            if (params[i] != null) {
                total = total.divide(params[i], 10, DEFAULT_ROUND_MODE);
            }
        }
        return total.setScale(DEFAULT_SCALE, DEFAULT_ROUND_MODE);
    }

    /**
     * double 减法
     *
     * @param params
     * @return
     */
    public static BigDecimal divide(double... params) {
        if (params == null || params.length == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal total = new BigDecimal(Double.toString(params[0]));
        for (int i = 1, len = params.length; i < len; i++) {
            total = total.divide(new BigDecimal(Double.toString(params[i])), 10, DEFAULT_ROUND_MODE);
        }
        return total.setScale(DEFAULT_SCALE, DEFAULT_ROUND_MODE);
    }

    /**
     * 四舍五入取得double的值
     *
     * @param value,如果是null，将转化成0
     * @param scale               表示表示需要精确到小数点以后几位
     * @return
     */
    public static BigDecimal round(BigDecimal value, int scale) {
        if (value == null) {
            return BigDecimal.ZERO;
        }
        return value.setScale(scale, DEFAULT_ROUND_MODE);
    }

    /**
     * 乘以1000
     *
     * @param value
     * @return
     */
    public static BigDecimal mulBy1000(BigDecimal value) {
        if (value == null) {
            return BigDecimal.ZERO;
        }
        return value.multiply(new BigDecimal(1000));
    }

    /**
     * 除以1000
     *
     * @param value
     * @return
     */
    public static BigDecimal divideBy1000(BigDecimal value) {
        if (value == null) {
            return BigDecimal.ZERO;
        }
        return value.divide(new BigDecimal(1000)).setScale(DEFAULT_SCALE, DEFAULT_ROUND_MODE);
    }

    /**
     * 相等 (equal)
     *
     * @param val1
     * @param val2
     * @return
     */
    public static boolean eq(BigDecimal val1, BigDecimal val2) {
        return val1 != null && val2 != null && val1.compareTo(val2) == 0;
    }

    /**
     * 不相等 (not equal)
     *
     * @param val1
     * @param val2
     * @return
     */
    public static boolean ne(BigDecimal val1, BigDecimal val2) {
        return !eq(val1, val2);
    }

    /**
     * 判断是否>0 (greater than)
     *
     * @param value
     * @return
     */
    public static boolean gtZero(BigDecimal value) {
        return value != null && value.compareTo(BigDecimal.ZERO) > 0;
    }

    /**
     * 判断是否>=0 (greater than or equal)
     *
     * @param value
     * @return
     */
    public static boolean geZero(BigDecimal value) {
        return value != null && value.compareTo(BigDecimal.ZERO) >= 0;
    }

    /**
     * 判断是否=0 (equal)
     *
     * @param value
     */
    public static boolean eqZero(BigDecimal value) {
        return value != null && value.compareTo(BigDecimal.ZERO) == 0;
    }

    /**
     * 判断是否<0 (less than)
     *
     * @param value
     */
    public static boolean ltZero(BigDecimal value) {
        return value != null && value.compareTo(BigDecimal.ZERO) < 0;
    }

    /**
     * 判断是否<=0 (less than or equal)
     *
     * @param value
     */
    public static boolean leZero(BigDecimal value) {
        return value != null && value.compareTo(BigDecimal.ZERO) <= 0;
    }

    /**
     * 获取最小值
     *
     * @param values
     * @return
     */
    public static BigDecimal min(BigDecimal... values) {
        if (values == null || values.length == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal result = values[0];
        for (int n = 1, len = values.length; n < len; n++) {
            result = result.min(values[n]);
        }
        return result;
    }

	/*public static void main(String[] args) {
        System.out.println(add(new BigDecimal("1.1"), new BigDecimal("2.2"), new BigDecimal("3.8")));
		System.out.println(add(1.1, 2.2, 3.8));
		
		System.out.println(subtract(new BigDecimal("3.3"), new BigDecimal("1"), new BigDecimal("2.1")));
		System.out.println(subtract(3.3, 1, 2.1));
		
		System.out.println(multiply(new BigDecimal("3.3"), new BigDecimal("1"), new BigDecimal("2.1")));
		System.out.println(multiply(3.3, 1, 2.1, 0.2));
		
		System.out.println(divide(new BigDecimal("3.3"), new BigDecimal("1"), new BigDecimal("2.1")));
		System.out.println(divide(3.3, 1, 2.1, 0.2));
	}*/

}
